package com.wish.house.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.dto.contract.HouseDto;
import com.wish.house.entity.House;
import com.wish.house.service.IHouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyk
 * @since 2022-08-19
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @Autowired
    private IHouseService houseService;


    @RequestMapping("/list")
    public List<HouseDto> list(){
        List<House> list = houseService.list();
        List<HouseDto> houseDtoList =new ArrayList<>();
        for (House house : list) {
            HouseDto houseDto = new HouseDto();
            BeanUtils.copyProperties(house,houseDto);
            houseDtoList.add(houseDto);
        }
        return houseDtoList;
    }

    @RequestMapping("/getHousesByAddress")
    public List<HouseDto> getHousesByName(String address) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("address",address);
        List<House> list = houseService.list(queryWrapper);
        List<HouseDto> houseDtoList =new ArrayList<>();
        for (House house : list) {
            HouseDto houseDto = new HouseDto();
            BeanUtils.copyProperties(house,houseDto);
            houseDtoList.add(houseDto);
        }
        return  houseDtoList;
    }

}
